<template>
    <!-- use the modal component, pass in the prop -->
    <modal v-if="stateManager.editDialogVisible"
           @cancel="closeDialog"
           @ok="saveModel"
           :modal-title="modalTitle"
           model-size="normal">
        <!--
          you can use custom content here to overwrite
          default content
        -->
        <vue-form>
            <div class="row">
                <div class="col-xs-12">
                    <vue-form-item label="学生姓名" :error-message="model.errors.student_name || errors.first('name')">
                        <input-text v-model:value="model.student_name"
                                    v-validate="'required'"
                                    label="学生姓名"
                                    el-name="name"></input-text>
                    </vue-form-item>
                    <vue-form-item label="学号" :error-message="model.errors.student_code || errors.first('number')">
                        <input-text v-model:value="model.student_code"
                                    v-validate="'required'"
                                    label="学号"
                                    el-name="number"></input-text>
                    </vue-form-item>
                    <vue-form-item label="领域" :error-message="model.errors.field || errors.first('field')">
                        <input-select v-model:value="model.field"
                                      v-validate="'required'"
                                      label="领域" :options="$store.state.fieldLists"
                                      el-name="field"></input-select>
                    </vue-form-item>
                    <vue-form-item label="班级" :error-message="model.errors.class || errors.first('class')">
                        <input-text v-model:value="model.class"
                                    v-validate="'required'"
                                    label="班级"
                                    el-name="class"></input-text>
                    </vue-form-item>


                </div>
            </div>
        </vue-form>


    </modal>


</template>

<script>
    export default {
        props: {
            stateManager: {
                type: Object
            }
        },
        data: function () {
            return {
                model: this.$modelDataSource({
                    url: 'api/classGroup/students',
                    dataKey: 'model',
                    attributes: {
                        id: 0,
                        class_group_name:'',
                        class_group_number:'',
                        peoples:'',
                        field:1
                    }
                })
            };
        },
        methods: {
            closeDialog: function () {
//                console.log(11);
                this.stateManager.editModelId = 0;
                this.stateManager.editDialogVisible = false;
            },
            saveModel: function () {
//                ladda.start();
                let self = this;
                self.validate(function () {
                    self.model.save(function () {
                        self.model.reset();
                        self.closeDialog();
                        self.stateManager.refreshList = true;
                        toastr.success('保存成功！');
                    }, function () {
                        if (self.model.errors.permissions_errors) {
                            toastr.error(self.model.errors.permissions_errors);
                        }
                    });
                });
            },

            validate: function (successCallback, failedCallback) {
                this.$validator.validateAll().then(() => {
                    // eslint-disable-next-line
                    console.log('验证通过');
                    typeof successCallback === 'function' && successCallback();
                }).catch(() => {
                    // eslint-disable-next-line
                    console.log('验证未通过');
                    typeof failedCallback === 'function' && failedCallback();
                });

            }
        },
        computed: {
            modalTitle: function () {
                if (this.model.id === 0) {
                    return '添加学生';
                } else {
                    return '编辑学生';
                }
            }
        },
        watch: {
            'stateManager.editModelId': function (newValue) {
                this.model.fetch(newValue);
            }
        }
    }
</script>